//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Security.Permissions;
namespace DotNetNuke.Common.Utilities
{
	partial class SiteMap : Framework.PageBase
	{
		const string SITEMAP_CHANGEFREQ = "daily";
		const string SITEMAP_PRIORITY = "0.5";
		const int SITEMAP_MAXURLS = 50000;
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			try {
				Response.ContentType = "text/xml";
				Response.ContentEncoding = Encoding.UTF8;
				BuildSiteMap(Response.Output, PortalSettings.PortalId);
			} catch (Exception exc) {
			}
		}
		private void BuildSiteMap(TextWriter textWriter, int PortalID)
		{
			XmlWriterSettings settings = new XmlWriterSettings();
			settings.Indent = true;
			settings.Encoding = Encoding.UTF8;
			settings.OmitXmlDeclaration = false;
			XmlWriter writer = XmlWriter.Create(textWriter, settings);
			writer.WriteStartElement("urlset", "http://www.sitemaps.org/schemas/sitemap/0.9");
			writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
			writer.WriteAttributeString("xsi", "schemaLocation", null, "http://www.sitemaps.org/schemas/sitemap/0.9");
			int intURLs = 0;
			string URL;
			TabController objTabs = new TabController();
			foreach (TabInfo objTab in objTabs.GetTabsByPortal(PortalID).Values) {
				if (objTab.IsDeleted == false && objTab.DisableLink == false && objTab.IsVisible && objTab.TabType == TabType.Normal && ((Null.IsNull(objTab.StartDate) == true || objTab.StartDate < DateTime.Now) && (Null.IsNull(objTab.EndDate) == true || objTab.EndDate > DateTime.Now))) {
					if (TabPermissionController.CanViewPage(objTab)) {
						if (intURLs < SITEMAP_MAXURLS) {
							intURLs += 1;
							URL = objTab.FullUrl;
							if (URL.ToLower().IndexOf(Request.Url.Host.ToLower()) == -1) {
								URL = Common.Globals.AddHTTP(Request.Url.Host) + URL;
							}
							BuildURL(writer, URL, objTab.SiteMapPriority);
						}
					}
				}
			}
			writer.WriteEndElement();
			writer.Close();
		}
		private void BuildURL(XmlWriter writer, string URL, float priority)
		{
			writer.WriteStartElement("url");
			writer.WriteElementString("loc", URL);
			writer.WriteElementString("lastmod", DateTime.Now.ToString("yyyy-MM-dd"));
			writer.WriteElementString("changefreq", SITEMAP_CHANGEFREQ);
			writer.WriteElementString("priority", priority.ToString("F02", CultureInfo.InvariantCulture));
			writer.WriteEndElement();
		}
	}
}
